home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: need for function prototypes
- Date: 27 Feb 1996 18:59:33 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb27115933@qcd.lanl.gov>
- References: <4gutho$o1a@mn5.swip.net>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: chris.rossall@mailbox.swipnet.se's message of Tue, 27 Feb 1996 12:45:14 GMT
-
- In article <4gutho$o1a@mn5.swip.net>
- chris.rossall@mailbox.swipnet.se (Chris Rossall) writes:
-
- CR: Hello I am having trouble understanding why I should use function
- CR: prototypes. I mean,if the function is defined before it is used,the
- CR: compiler should have enough information about the parameters to
- CR: produce correct code anyway.
-
- Point 1:
-
- The calling sequence of a function with a prototype is often different
- from the type it seems to be declared with. Thus in
-
- int f(x)
- char x;
- { /* definition */ }
-
- The calling sequence actually passes an int. So, if you wrote,
-
- char a;
- f(a);
-
- The compiler would have to change a to an int before passing. This it
- does automatically, if it does not find a prototype.
-
- On the other hand,
-
- int f(char x) { /* definition */ }
-
- needs x to be passed as a char. So, this time, before you write
-
- char a;
- f(a);
-
- you must make sure that the prototype is visible, so that the compiler
- does not change a to an int before passing.
-
- Point 2: There is no way to use the calling convention of a variable
- argument function (e.g. printf) without a prototype.
-
- Point 3: Mistakes happen to everyone. A compilers duty is to check for
- stupid user errors. Correctly used prototypes specifically ask the
- compiler to check for user errors.
-
- Point 4: When one uses a prototype, some automatic conversions take
- place. These are useful. Thus, after `int f(char *x);' f(NULL) means
- f((char*)NULL). In fact use of uncast NULL as a parameter to a
- non-prototyped function is always a mistake.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-